package cn.com.nes.site.service.system.ope.impl;

import cn.com.nes.common.em.platform.OpeMethodEnum;
import cn.com.nes.common.exception.IllegalBusinessException;
import cn.com.nes.common.util.HttpUtils;
import cn.com.nes.site.service.system.ope.OpeService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Set;

/**
 * 运营平台Service
 *
 * @Author: ShenWei
 * @Date: 2024/08/16/13:43
 */
@Slf4j
@Service
public class OpeServiceImpl implements OpeService {


    @Override
    public void restartDtu(String dtuId) {
        if (StrUtil.isBlank(dtuId)) {
            throw new IllegalBusinessException("dtuId must not null");
        }

        JSONObject paramJson = new JSONObject();
        paramJson.put("dtuId", dtuId);

        String apiUrl = OpeMethodEnum.RESTART_DTU.url();

        // 运营平台内部放行接口,无需token
        String result = HttpUtils.sendPost(apiUrl, paramJson.toJSONString(), new HashMap<>());
        log.info("Api回复 - {} : {}", apiUrl, result);
    }

    @Override
    public void restartDtu(Set<String> dtuIdSet) {
        if (CollUtil.isEmpty(dtuIdSet)) {
            return;
        }

        for (String dtuId : dtuIdSet) {
            if (StrUtil.isBlank(dtuId)) {
                log.error("dtuId must not empty");
                continue;
            }
            this.restartDtu(dtuId);
        }
    }

    @Override
    public void reloadProtocolInfo(String protocolId) {
        if (StrUtil.isBlank(protocolId)) {
            throw new IllegalBusinessException("protocolId must not null");
        }

        JSONObject paramJson = new JSONObject();
        paramJson.put("protocolId", protocolId);

        String apiUrl = OpeMethodEnum.RELOAD_PROTOCOL_INFO.url();

        // 运营平台内部放行接口,无需token
        String result = HttpUtils.sendPost(apiUrl, paramJson.toJSONString(), new HashMap<>());
        log.info("Api回复 - {} : {}", apiUrl, result);
    }
}
